package com.vito.oa.email.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.oa.email.contact.dao.ExternalContactsGroupMapper;
import com.vito.oa.email.contact.dao.ExternalContactsMapper;
import com.vito.oa.email.model.ExternalContacts;
import com.vito.oa.email.model.ExternalContactsGroup;
import com.vito.oa.email.service.ExternalContactsGroupService;

@Service
public class ExternalContactsGroupIserviceImpl implements ExternalContactsGroupService {

	@Autowired
	private ExternalContactsGroupMapper externalContactsGroupMapper;
	
	@Autowired
	private ExternalContactsMapper externalContactsMapper;

	public List<ExternalContactsGroup> getGroupByUserId(String userId) {
		return externalContactsGroupMapper.getGroupByUserId(userId);
	}

	public boolean addContactsGroup(ExternalContactsGroup group) {
		return externalContactsGroupMapper.addContactsGroup(group) > 0;
	}

	public boolean updateContactsGroup(ExternalContactsGroup group) {
		return externalContactsGroupMapper.updateContactsGroup(group) > 0;
	}

	public ExternalContactsGroup getGroupByName(ExternalContactsGroup group) {
		return externalContactsGroupMapper.getGroupByName(group);
	}

	public List<ExternalContactsGroup> getGroupTreeByUserId(String userId) {
		List<ExternalContactsGroup> list = externalContactsGroupMapper.getGroupByUserId(userId);
		ExternalContacts contacts = new ExternalContacts();
		contacts.setUserId(userId);
		for (ExternalContactsGroup group : list) {
			contacts.setGroupId(group.getGroupId());
			group.setPersonCount(externalContactsMapper.getContactsCountByGroupId(contacts));
		}
		return list;
	}

	public List<ExternalContactsGroup> getEmailContactsTree(String userId) {
		List<ExternalContactsGroup> list = externalContactsGroupMapper.getEmailGroupTree(userId);
		ExternalContacts contacts = new ExternalContacts();
		contacts.setUserId(userId);
		for (ExternalContactsGroup group : list) {
			contacts.setGroupId(group.getId());
			group.setGlevel(1);
			group.setChildren(externalContactsMapper.getEmailContactsTree(contacts));
		}
		return list;
	}

}
